<template>
  <div id="app">
    <div class="my-head">
      <el-button type="success" @click="sendVue2Age">向vue2传age值</el-button>
      <el-tag type="warning">vue2传的name值：{{name}}</el-tag>

      <el-button type="info" @click="sendVue3User">向vue3传对象user的num值</el-button>
      <el-button type="warning" @click="setGlobalTime">设置全局时间</el-button>
      <el-tag type="danger">全局时间为{{globalTime}}</el-tag>
    </div>
    <div class="my-body">
      <div class="menu-con">
      <el-menu
        class="el-menu-vertical-demo"
        :router='true'
        :default-active='activeIndex'
      >
        <el-menu-item index="/">
          <i class="el-icon-document"></i>
          <span slot="title">首页</span>
        </el-menu-item>
        <el-menu-item index="/vue2/">
          <i class="el-icon-document"></i>
          <span slot="title">vue2</span>
        </el-menu-item>
        <el-menu-item index="/vue3/">
          <i class="el-icon-setting"></i>
          <span slot="title">vue3</span>
        </el-menu-item>
      </el-menu>
    </div>
      <div class="router-con">
        <router-view/>
      </div>
    </div>
  </div>
</template>

<script>
import microApp from '@micro-zoe/micro-app'
import moment from 'moment'
export default {
  name: 'App',
  data () {
    return {
      activeIndex: '/',
      age: 1,
      name: 'zs',
      user: {
        num: 1
      },
      globalTime: ''
    }
  },
  mounted () {
    microApp.addDataListener('vue2Page', (data) => {
      console.log('vue2穿的值为', data)
      this.name = data.name
    })
    microApp.addGlobalDataListener((data) => {
      console.log('全局的值为', data)
      this.globalTime = data.globalTime
    })
  },
  methods: {
    sendVue2Age () {
      this.age++
      microApp.setData('vue2Page', { age: this.age })
    },
    sendVue3User () {
      this.user.num++
      microApp.setData('vue3Page', { user: this.user })
    },
    setGlobalTime () {
      const time = moment().format('YYYY-MM-DD HH:mm:ss')
      microApp.setGlobalData({ globalTime: time })
    }
  },
  watch: {
    $route: function (newRoute, oldRoute) {
      this.activeIndex = `/${newRoute.name}/`
    }
  }
}
</script>

<style>
  #app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #2c3e50;
    background: #fff;
    width: 100%;
    /* padding: 10px; */
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }

  .my-head{
    width: 100%;
    height: 80px;
    background: skyblue;
  }

  .my-body{
    display: flex;
  }

  .menu-con  {
    width: 200px;
  }

  .router-con  {
    flex: 1;
  }

  body {
    margin: 0;
  }
</style>
